<template>
	<view>

		<view class="topbar-wrapper">
			<view class="topbar-box">
				<view class="topbar-item" :class="{ 'active': ( activeType==item.type ) }"
					v-for="(item, index) in benefitsTypes" :key="index" @click="changeBenefitsType(item)">
					{{ item.name }}
				</view>
			</view>
		</view>

		<view class="order-wrapper">
			<view class="order-list">
				<u-list :height="listWrapperH" @scrolltolower="handleLoadMore">
					<u-list-item v-for="(item, index) in list" :key="index">
						<view class="order-item">
							<view class="order-item_bd">
								<view class="order-title">{{ item.product.productName }}</view>
								<view class="order-status">{{ item.orderStatus==20|| item.orderStatus==30?"-1":"+1" }}
								</view>
							</view>
							<view class="order-datetime">{{ item.payTime | formatDate }}</view>
						</view>
					</u-list-item>
				</u-list>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getBenefitsList
	} from '@/api/user.js';
	import {
		dateFormat
	} from '@/utils/util.js';

	const benefitsTypes = [{
			name: '活动权益',
			type: 'activity',
			value: 10,
		},
		{
			name: '营地权益',
			type: 'camp',
			value: 20,
		}
	]
	export default {
		filters: {
			formatDate(date) {
				return dateFormat('YYYY-mm-dd HH:MM', new Date(date))
			},
		},
		data() {
			return {
				// 页面传参
				options: {},
				// 顶部导航高度
				pageHeaderH: 0,
				// 顶部tabs高度
				tabsH: 0,
				// 列表滚动高度
				listWrapperH: 0,
				// 权益类型
				benefitsTypes: benefitsTypes,
				// 选中的权益类型
				activeType: 'activity',
				// 列表
				list: [],
				// 查询参数
				listParams: {
					productType: 10,
					pageNum: 0,
					pageSize: 10,
				},
				// 是否没有更多了
				isNoMore: false,
			};
		},
		onReady() {
			let that = this
			// 重置列表滚动高度
			let selector = uni.createSelectorQuery().in(this);
			selector.select(".topbar-wrapper").boundingClientRect(function(data) {
				that.listWrapperH = (uni.getSystemInfoSync().windowHeight - data.height) + 'px'
			}).exec()
		},
		onLoad(options) {
			let that = this
			that.options = options
			if (options && options.type) {
				that.activeType = options.type
				that.benefitsTypes.forEach(item => {
					if (item.type == options.type) {
						that.listParams.productType = item.value
					}
				})
			}
		},
		onShow() {
			this._getBenefitsList(true)
		},
		methods: {
			// 切换收藏类型查询
			changeBenefitsType({
				type,
				value
			}) {
				this.activeType = type
				this.listParams.productType = value
				this._getBenefitsList(true)
			},

			// 获取收藏列表
			_getBenefitsList(refresh) {
				let that = this
				if (refresh) {
					that.listParams.pageNum = 1
					that.list = []
					that.isNoMore = false
				} else if (that.isNoMore) {
					that.$util.showToast('没有更多了')
					return
				}
				uni.showLoading()
				getBenefitsList(that.listParams.productType, that.listParams).then(res => {
					that.list = that.list.concat(res.rows)
					if (res.rows.length == that.listParams.pageSize) {
						that.listParams.pageNum++
					} else {
						that.isNoMore = true
					}
				}).finally(res => {
					uni.hideLoading()
				})
			},

			// 加载更多
			handleLoadMore() {
				this._getBenefitsList()
			},

		}
	}
</script>

<style lang="scss" scoped>
	.topbar-wrapper {
		.topbar-box {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 96rpx;
			display: flex;
			align-items: center;
			background-color: #F7F5F0;
			z-index: 999;
		}

		.topbar-item {
			flex: 1;
			color: $uni-text-color;
			opacity: .4;
			font-size: 32rpx;
			text-align: center;

			&.active {
				opacity: 1;
				font-weight: bold;
			}
		}

		&::after {
			content: '';
			display: block;
			width: 100%;
			height: 96rpx;
		}
	}

	.order-list {
		.order-item {
			background-color: rgba(247, 245, 240, 1);
			padding: 32rpx;
			margin-top: 16rpx;

			&_bd {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				font-size: 32rpx;

				.order-title {
					width: 480rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.order-status {
					font-weight: bold;
				}
			}

			.order-datetime {
				font-size: 24rpx;
				color: rgba(26, 19, 17, .6);
				margin-top: 8rpx;
			}
		}
	}
</style>
